<?php

namespace App\Modules\GrpcApi\Controller;

use Api\ApiRequest;
use Api\ApiResult;
use App\Modules\BaseController;
use App\Modules\GrpcApi\Request\GetBusLineRequest;
use App\Modules\GrpcApi\Service\ApiService;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Hyperf\Di\Annotation\Inject;

class ApiController extends BaseController
{
    /**
     * @Inject
     */
    protected ApiService $service;

    /**
     * @Inject
     * @var ValidatorFactoryInterface
     */
    protected ValidatorFactoryInterface $validatorFactory;

    /**
     * @param GetBusLineRequest $request
     * @return ResponseInterface
     */
    public function getBusLine(GetBusLineRequest $request): ResponseInterface
    {
        return $this->response->success($this->service->getBusLine($request->start_station, $request->end_station, $request->city));
    }

    /**
     * @param ApiRequest $request
     * @return ApiResult
     */
    public function getBusLineByGrpc(ApiRequest $request): ApiResult
    {
        $service = make(ApiService::class);
        $data = $service->getBusLine(
            $request->getStartStation(),
            $request->getEndStation(),
            $request->getCity()
        );
        $response = new ApiResult();
        $response->setStartStation($request->getStartStation());
        $response->setEndStation($request->getEndStation());
        $response->setRoutes($data);
        return $response;
    }
}